# 多元线性回归实战
# 多元线性回归实战-加载数据集
from sklearn import datasets
from sklearn.preprocessing import StandardScaler # 用于数据归一化
from sklearn.model_selection import train_test_split # 用于数据集划分
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error,r2_score
from joblib import dump,load
from sklearn.ensemble import RandomForestRegressor # 随机森林算法
from sklearn.metrics import mean_absolute_error,r2_score
# 加载数据集
#house = datasets.fetch_california_housing()
# 若数据集加载失败，可使用其他数据集
house = datasets.load_iris()
print(dir(house))
# 查看data数据和target数据
X, y = house.data,house.target
print(X.shape,y.shape) # 显示特征数组形状和标签数组形状
print(X[:5],y[:5]) # 显示前5个样本特征和标签

X_std = StandardScaler().fit_transform(X) # 数据预处理-标准化操作
X_train,X_test,y_train,y_test = train_test_split(X_std,y,test_size=0.3,random_state=1) # 划分训练集与测试集

LR_regr = LinearRegression() # 实例化，定义一个线性回归器
LR_regr.fit(X_train,y_train) # 应用训练集进行模型训练
y_predict = LR_regr.predict(X_test) # 对测试集进行预测
plt.figure(figsize=(16,4)) # 对比预测结果和真实值（Grand_truth）,对前30个预测结果可视化
plt.plot(range(30),y_test[:30],color='black',label="truth")
plt.plot(range(30),y_predict[:30],color='red',label="predict")
plt.legend()

mse = mean_squared_error(y_test,y_predict)
r_2 = r2_score(y_test,y_predict)
print('Mean squared error: %.3f'%(mse))
print('Coefficient of determination: %.3f'%(r_2))

score = LR_regr.score(X_test,y_test) # 测试集的特征和标签作为参数
print('%.3f'%(score))

dump(LR_regr,'house_model.joblib') #模型持久化

model = load('house_model.joblib')
y_pred = model.predict(X_test)
print(y_pred)
# 定义随机森林回归器
RF_regr = RandomForestRegressor(n_estimators=50,random_state=1)
RF_regr.fit(X_train,y_train) # 训练模型
RF_y_pred = RF_regr.predict(X_test) # 预测
# 模型评估
mse = mean_squared_error(y_test,RF_y_pred)
r_2 = r2_score(y_test,RF_y_pred)
print('Mean squared error: %.3f'%(mse))
print('Coefficient of determination: %.3f'%(r_2))